<!-- 新增、编辑 -->

<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :destroy-on-close="true"
    title="版本信息"
    width="450px"
    @close="emits('closeDialog')"
  >
    <el-form ref="dialogFormRef" :model="dialogForm">
      <el-form-item label="标题" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.title" />
      </el-form-item>
      <el-form-item label="说明" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.description" />
      </el-form-item>
      <el-form-item label="位置" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.position" />
      </el-form-item>

      <el-form-item label="语言" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.lang" />
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <el-upload
          :action="uploadimgUrl"
          :data="{ project_code: 'mobybox' }"
          :show-file-list="false"
          :on-success="handleUploaderImgSuccess"
          :limit="1"
          :headers="TOKENT_HEADER"
          class="avatar-uploader"
        >
          <!-- :src="dialogForm.imageUrl" -->
          <img
            v-if="dialogForm.image"
            :src="dialogForm.image_show"
            class="avatar"
          />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
      <!-- <el-form-item label="图片说明" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.description" />
      </el-form-item> -->
      <!-- <el-form-item label="跳转地址" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.url" />
      </el-form-item> -->
      <el-form-item label="跳转类型" :label-width="formLabelWidth">
        <el-select v-model="dialogForm.go_type" placeholder="跳转类型">
          <el-option label="go_app" value="go_app" />
          <el-option label="go_webview" value="go_webview" />
          <el-option label="go_h5" value="go_h5" />
        </el-select>
      </el-form-item>

      <el-form-item label="View名称" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.view_name" />
      </el-form-item>
      <el-form-item label="跳转参数" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.params" />
      </el-form-item>

      <el-form-item label="排序数字" :label-width="formLabelWidth">
        <el-input v-model="dialogForm.priority" />
      </el-form-item>
      <el-form-item label="上架状态" :label-width="formLabelWidth">
        <el-select v-model="dialogForm.status" placeholder="上架状态">
          <el-option label="否" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
      </el-form-item>

      <el-form-item label="跳转是否需要登录" :label-width="formLabelWidth">
        <el-select v-model="dialogForm.is_login" placeholder="跳转是否需要登录">
          <el-option label="否" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
      </el-form-item>

      <el-form-item label="Banner ID" style="display: none">
        <el-input v-model="dialogForm.banner_id" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('cancel')">取消</el-button>
        <el-button
          type="primary"
          @click="submit(dialogForm)"
          :loading="btnLoading"
        >
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from "vue";
import { uploadimgUrl } from "@/api/system/uploadimg";
import { TOKENT_HEADER } from "@/config/api-constant/system";
const dialogFormRef = ref(null);

defineProps({
  dialogFormVisible: {
    type: Boolean,
    default: false,
  },
  dialogForm: {
    type: Object,
    default: {
      banner_id: "",
      name: "",
      position: "home",
      lang: "en",
      image: "",
      // url: "",
      params: "",
      go_type: "",
      priority: "",
      status: "",
      is_login: "",
      imageUrl: "",
      description: "",
      image_show: "",
      view_name: "",
    },
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
});
const emits = defineEmits([
  "closeDialog",
  "cancel",
  "submitForm",
  "handleUploaderImgRequest",
]);

const formLabelWidth = "140px";

// 提交
const submit = (dialogForm) => {
  dialogFormRef.value
    .validate()
    .then(async () => {
      try {
        emits("submitForm", dialogForm);
      } catch (error) {
        console.log(error);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};
const handleUploaderImgSuccess = (res) => {
  emits("handleUploaderImgRequest", res, "image", "image_show");
};
</script>
